$(function ()
{
    var grid = new TableList();
    grid.init({
        url : adminBasePath + "category/list",
        searchForm : "#searchForm",
        tableDiv : "#tableDiv",
        fields : [
            {
                name : "id",
                display : "id",
                width : 80
            },
            {
                name : "name",
                display : "分类名"
            },
            {
                name : "slug",
                display : "名称"
            },
            {
                name : "pname",
                display : "父类别"
            }
        ]
    });

    $("div.panel-heading").find("button").bind("click", function ()
    {
        var btn = $(this);
        var itemId = btn.data("id");

        toolbarClick(itemId);
    });

    function toolbarClick(itemId)
    {
        var selected;
        switch(itemId)
        {
            case 'add':
                location.href = adminBasePath + "post/newPost";
                break;
            case 'modify':
                location.href = adminBasePath + "menu/newMenu";
                break;
            case 'delete':
                selected = grid.getSelected();
                if (!selected)
                {
                    return common.info("请选择行!");
                }

                common.confirm("确定删除吗?", del, selected);
                break;
        }
    }

    function del(selected)
    {
        $.ajax({
            url : adminBasePath + "category/delete.do",
            data : {id : selected.id},
            dataType : 'json',
            type : 'post',
            success : function (result)
            {
                var msg = result.message;
                if (result.isError)
                {
                    common.info(msg);
                }
                else
                {
                    common.info(msg);
                    grid.ajax();
                }
            },
            error : function ()
            {
                console.log(arguments);
            }
        });
    }
});